package com.lagou.com.lagou.homework.Modules4.item4;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {

        PrintStream ps = null;
        ObjectOutputStream oos = null;
        Socket s = null;
        ObjectInputStream ois = null;
        //Scanner sc = null;
        //BufferedReader br = null;
        try {
            s = new Socket("192.168.1.4", 8888);
            System.out.println("连接成功");

            //Thread.sleep(1000);
            //客户端向服务器发送：
            oos = new ObjectOutputStream(s.getOutputStream());
            //oos.writeObject(new UserMessage("pass", new User("root", "123456")));
            UserMessage um = new UserMessage("check", new User("admin", "223456"));
            oos.writeObject(um);
            //ps = new PrintStream(s.getOutputStream());
            //ps.println("test");
            ois = new ObjectInputStream(s.getInputStream());
            UserMessage obj = (UserMessage) ois.readObject();
            if (obj.getCheck().equals("success")){
                System.out.println("登录成功！");
            }else{
                System.out.println("登录失败！");
            }

        } catch (IOException | ClassNotFoundException /*| InterruptedException*/ e) {
            e.printStackTrace();
        }finally {
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            /*if (null != ps) {
                ps.close();
            }*/
        }
    }

}
